package com.lsy.controller.back;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lsy.entity.UserCustom;
import com.lsy.service.impl.UserServiceImpl;

@Controller
@RequestMapping("/back/user")
public class UserController {

	@Autowired
	private UserServiceImpl userService;

	// 跳转都添加用户表单
	@RequestMapping("/addUserForm")
	public ModelAndView addUserForm() {
		// 创建modelAndView准备填充数据、设置视图
		ModelAndView modelAndView = new ModelAndView();
		try {
			// 指定视图
			modelAndView.setViewName("/BackEnd/AddUser");

		} catch (Exception e) {
			e.printStackTrace();
		}

		return modelAndView;
	}

	// 添加用户提交
	@RequestMapping("/addUser")
	@ResponseBody
	public String addUser(HttpServletRequest request, Model model, UserCustom userCustom) {
		String result = "{\"code\":\"fail\"}";
		try {
			userCustom.setUregtime(new Date());
			userService.AddUser(userCustom);
			result = "{\"code\":\"success\"}";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	// 查看用户列表
	@RequestMapping("/queryUser")
	public ModelAndView queryUser() throws Exception {

		// 用户列表
		List<UserCustom> usersList = userService.findUserList(null);

		// 创建modelAndView准备填充数据、设置视图
		ModelAndView modelAndView = new ModelAndView();

		// 填充数据
		modelAndView.addObject("usersList", usersList);

		// 指定视图
		modelAndView.setViewName("/BackEnd/usersList");

		return modelAndView;
	}

	@RequestMapping("/queryById")
	@ResponseBody
	public UserCustom queryById(int id) {
		UserCustom user = null;
		try {
			user = userService.findUserById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	// 修改用户信息
	@RequestMapping(value = "/editUser", method = { RequestMethod.POST, RequestMethod.GET })
	public String editUser(Model model, Integer id) throws Exception {
		// 属性名字要和前台传来的一致
		UserCustom userCustom = userService.findUserById(id);

		model.addAttribute("userCustom", userCustom);

		return "/BackEnd/editUser";

	}

	// 更新用户信息
	@RequestMapping("/editUserSubmit")
	@ResponseBody
	public String editUserSubmit(HttpServletRequest request, UserCustom userCustom) throws Exception {
		String result = "{\"code\":\"fail\"}";
		userService.UpdateUserInfo(userCustom.getUserid(), userCustom);
		result = "{\"code\":\"success\"}";
		return result;

	}

	// 删除用户信息
	@RequestMapping("/deleteUser")
	@ResponseBody
	public String deleteUser(HttpServletRequest request, Integer id) throws Exception {

		userService.deleteUser(id);

		return "{\"code\":\"success\"}";

	}

}
